package com.esunny.resourcevisualization.service.impl;

import com.esunny.resourcevisualization.domain.entity.ResourceType;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.manage.ResourceManage;
import com.esunny.resourcevisualization.manage.ResourceTypeManage;
import com.esunny.resourcevisualization.service.ResourceTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName ResourceTypeServiceImpl
 * @Description 资源类别逻辑处理
 * @Author baochencong
 * @Date 2020/9/28 10:07
 */

@Service
public class ResourceTypeServiceImpl implements ResourceTypeService {

    @Resource
    private ResourceTypeManage resourceTypeManage;

    @Resource
    private ResourceManage resourceManage;

    @Override
    public ReturnStructure addResourceType(ResourceType resourceType) {
        String name = resourceType.getResourceTypeName();
        String title = resourceType.getResourceTypeTitle();
        if (isExistedResourceTypeByName(name)){
            return ReturnStructure.ERROR("资源类别英文名已经存在",null);
        }else if (!isNotExistedResourceTypeByTitle(title)){
            return ReturnStructure.ERROR("资源类别中文名已经存在",null);
        } else{
            return resourceTypeManage.addResourceType(resourceType);
        }
    }

    @Override
    public ReturnStructure deleteResourceTypeByName(String name) {
        if (isExistedResourceTypeByName(name)){
            return resourceManage.deleteResourceTypeInfoByName(name);
        }
        return ReturnStructure.ERROR("该资源类别不存在",null);
    }

    @Override
    public ReturnStructure updateResourceTypeByName(ResourceType resourceType) {
        String title = resourceType.getResourceTypeTitle();
        if (isNotExistedResourceTypeByTitle(title)){
            return resourceTypeManage.updateResourceTypeByName(resourceType);
        }
        return ReturnStructure.ERROR("该资源类别中文名已存在",null);
    }

    @Override
    public List<ResourceType> getResourceTypeList() {
        return resourceTypeManage.getResourceTypeList();
    }

    @Override
    public ResourceType getResourceTypeByName(String name) {
        return resourceTypeManage.getResourceTypeByName(name);
    }

    /**
     * 通过资源类别英文名判断资源类别是否存在
     *
     * @param name 资源类别英文名
     * @return 是否存在
     */
    private boolean isExistedResourceTypeByName(String name){
        return resourceTypeManage.getResourceTypeByName(name) != null;
    }

    /**
     * 通过资源类别中文名判断资源类别是否存在
     * @param title 资源类别中文名
     * @return 是否存在
     */
    private boolean isNotExistedResourceTypeByTitle(String title){
        return resourceTypeManage.getResourceTypeByTitle(title) == null;
    }
}
